<uieip type="reg" value="resource,data"></uieip>
<script runat="server" src="/handler/js/rsa.js"></script>
<script runat="server">
    response.contentType('application/json');

    var res = get_service_info();
    if (res.error != 0) {
        response.body(JSON.stringify(res));
    } else {
        response.body(JSON.stringify(res.value));
    }

    function get_service_info() {
        var msg = request.body(true);
        var res = resource.actionMsgByString(msg);
        if (res.error != 0) {
            return res;
        }

        res = resource.rsaDecryptCSharp(res.content, privateKey);
        if (res.error != 0) {
            return res;
        }

        var dd = JSON.parse(res.value);
        var name = dd.name;
        var ip = request.ip();

        // name = "00:1C:42:B3:70:D8";

        res = data.select("yqbase", "licService", "domainName='" + name + "'", "");
        if (res.error != 0) {
            return res;
        }
        if (res.rows.length == 0) {
            update_link(row.sid, 100, ip)
            return {
                error: 100,
                info: "服务[" + name + "]未注册. 请登录 www.yqsite.com 进行注册。"
            }
        }

        var row = res.rows[0];

        // 检查是否已注销
        if (row.isdel === true) {
            update_link(row.sid, 101, ip)
            return {
                error: 101,
                info: "服务[" + name + "]注销状态，请登录 www.yqsite.com 续期服务。"
            }
        }

        //row.duedate = "2025-12-16 00:00:00";
        // 检查是否已到期
        if (row.duedate) {
            var duedate = new Date(row.duedate);
            var currentDate = new Date();
            if (currentDate > duedate) {
                var duedateStr = row.duedate.split(' ')[0]; // 只取日期部分
                update_link(row.sid, 102, ip)
                return {
                    error: 102,
                    info: "服务[" + name + "]已过期[" + duedateStr + "]，请登录 www.yqsite.com 续期服务。"
                }
            }
        }

        var newrow = {
            "error": 0,
            "sId": row.id,
            "name": name,
            "describe": row.discription,
            "app": row.appurl,
            "sso": row.ssourl,
            "eip": row.eipurl,
            "web": row.weburl,
            "attr": row.attr,
            "info": row.remark,
            "isdel": row.isdel,
            "dueDate": row.duedate.split(' ')[0], // 只取日期部分
            "appDate": row.appdate.split(' ')[0], // 只取日期部分
        };

        update_link(row.sid, 0, ip)

        return {
            error: 0,
            info: "success",
            value: newrow
        }
    }

    function update_link(sid, error, ip) {
        var linktime = resource.timeToISOString(new Date());

        var res = data.action("yqbase", "licService", [{
            "action": "upp_",
            row: {
                "sid": sid,
                "linkcorrect": error,
                "linktime": linktime,
                "ip": ip
            }
        }]);
        if (res.error != 0) {
            return res;
        }

        res = data.action("yqbase", "licServiceLog", [{
            "action": "add",
            row: {
                "pid": sid,
                "linkcorrect": error,
                "linktime": linktime,
                "ip": ip
            }
        }]);

        return res;
    }
</script>